package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @DubboReference
    RoleService roleService;
    //角色界面数据 分页查询
    @RequestMapping("/findAll")
    public Result findAll(QueryPageBean queryPageBean){
        PageResult pageResult = roleService.findAll(queryPageBean);
        if (pageResult!=null){
            return new Result(true,MessageConst.ACTION_SUCCESS,pageResult);
        }
        return new Result(false,MessageConst.ACTION_FAIL);
    }

    //新增

    //回显菜单权限
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        List<Menu> menus= roleService.findAllMenu();
        if(menus!=null){
            return new Result(true,MessageConst.ACTION_SUCCESS,menus);
        }
        return new Result(false,MessageConst.ACTION_FAIL);
    }
    //回显CRUD权限
    @RequestMapping("/findAllPermission")
    public Result findAllPermission(){
        List<Permission> list = roleService.findAllPermission();
        if (list!=null){
            return new Result(true,MessageConst.ACTION_SUCCESS,list);
        }
        return new Result(false,MessageConst.ACTION_FAIL);
    }
    //新增功能实现
    @RequestMapping("/add")
    public Result add(@RequestBody Role role ,Integer[] permissions, Integer[] menus){
        roleService.add(permissions,menus,role);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        roleService.delete(id);
       return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    //编辑

    //编辑角色回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role = roleService.findById(id);
        if (role==null){
            return new Result(false,MessageConst.ACTION_FAIL);
        }
        return new Result(true,MessageConst.ACTION_SUCCESS,role);
    }
    //编辑 角色关联管理菜单的id
    @RequestMapping("/findMenusByRoleId")
    public Result findMenusByRoleId(Integer id){
        List<Integer> menuIds = roleService.findMenusByRoleId(id);
        if (menuIds==null){
            return new Result(false,MessageConst.ACTION_FAIL);
        }
        return new Result(true,MessageConst.ACTION_SUCCESS,menuIds);
    }
    //编辑 角色关联管理权限的id
    @RequestMapping("/findPermissionsByRoleId")
    public Result findPermissionsByRoleId(Integer id){
      List<Integer> PermissionIds = roleService.findPermissionsByRoleId(id);
      if (PermissionIds==null){
          return new Result(false,MessageConst.ACTION_FAIL);
      }
     return  new Result(true,MessageConst.ACTION_SUCCESS,PermissionIds);
    }
    //编辑 操作
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissions,Integer[] menus){
        roleService.edit(role,permissions,menus);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }


}